package com.zxn.tablayoutsamples.ui

import android.content.Context
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import com.zxn.tablayoutsamples.R
import com.zxn.tablayout.listener.CustomTabEntity
import androidx.viewpager.widget.ViewPager
import com.zxn.tablayout.CommonTabLayout
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.zxn.tablayoutsamples.entity.TabEntity
import com.zxn.tablayoutsamples.utils.ViewFindUtils
import com.zxn.tablayout.listener.OnTabSelectListener
import com.zxn.tablayout.utils.UnreadMsgUtils
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import androidx.fragment.app.FragmentPagerAdapter
import com.zxn.tablayoutsamples.entity.MenuTabEntity
import java.util.*

class CommonTabActivity : AppCompatActivity() {
    var mRandom = Random()
    private val mContext: Context = this
    private val mFragments = ArrayList<Fragment>()
    private val mFragments2 = ArrayList<Fragment?>()
    private val mTitles = arrayOf("待接单", "配送异常", "进行中", "退款中", "已完成")
    private val mIconUnselectIds = intArrayOf(
        R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
        R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect, R.mipmap.tab_more_unselect
    )
    private val mIconSelectIds = intArrayOf(
        R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
        R.mipmap.tab_contact_select, R.mipmap.tab_more_select, R.mipmap.tab_more_select
    )
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private val mTabEntities11 = ArrayList<CustomTabEntity>()

    private var mDecorView: View? = null
    private var mViewPager: ViewPager? = null
    private var mTabLayout_1: CommonTabLayout? = null
    private var mTabLayout_2: CommonTabLayout? = null
    private var mTabLayout_3: CommonTabLayout? = null
    private var mTabLayout_4: CommonTabLayout? = null
    private var mTabLayout_5: CommonTabLayout? = null
    private var mTabLayout_6: CommonTabLayout? = null
    private var mTabLayout_7: CommonTabLayout? = null
    private var mTabLayout_8: CommonTabLayout? = null
    private var mTabLayout_9: CommonTabLayout? = null
    private var mTabLayout_10: CommonTabLayout? = null
    private var mTabLayout_11: CommonTabLayout? = null
    private var mTabLayout_msg: CommonTabLayout? = null

    private val ctlLottie: CommonTabLayout by lazy {
        findViewById(R.id.ctlLottie)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_common_tab)

        for (title in mTitles) {
            mFragments.add(SimpleCardFragment.getInstance("Switch ViewPager $title"))
            mFragments2.add(SimpleCardFragment.getInstance("Switch Fragment $title"))
        }
        for (i in mTitles.indices) {
            mTabEntities.add(TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]))
            if (i < 2) {
                mTabEntities11.add(TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]))
            }
        }
        mDecorView = window.decorView
        mViewPager = ViewFindUtils.find(mDecorView, R.id.vp_2)
        mViewPager?.adapter = MyPagerAdapter(
            supportFragmentManager
        )
        /** with nothing  */
        mTabLayout_1 = ViewFindUtils.find(mDecorView, R.id.tl_1)
        /** with ViewPager  */
        mTabLayout_2 = ViewFindUtils.find(mDecorView, R.id.tl_2)
        /** with Fragments  */
        mTabLayout_3 = ViewFindUtils.find(mDecorView, R.id.tl_3)
        /** indicator固定宽度  */
        mTabLayout_4 = ViewFindUtils.find(mDecorView, R.id.tl_4)
        /** indicator固定宽度  */
        mTabLayout_5 = ViewFindUtils.find(mDecorView, R.id.tl_5)
        /** indicator矩形圆角  */
        mTabLayout_6 = ViewFindUtils.find(mDecorView, R.id.tl_6)
        /** indicator三角形  */
        mTabLayout_7 = ViewFindUtils.find(mDecorView, R.id.tl_7)
        /** indicator圆角色块  */
        mTabLayout_8 = ViewFindUtils.find(mDecorView, R.id.tl_8)
        /** indicator圆角色块2  */
        mTabLayout_9 = ViewFindUtils.find(mDecorView, R.id.tl_9)
        mTabLayout_10 = ViewFindUtils.find(mDecorView, R.id.tl_10)
        mTabLayout_11 = ViewFindUtils.find(mDecorView, R.id.tl_11)
        mTabLayout_msg = ViewFindUtils.find(mDecorView, R.id.ctl_msg)
        mTabLayout_1?.setTabData(mTabEntities)
        tl_2()
        mTabLayout_3?.setTabData(mTabEntities, this, R.id.fl_change, mFragments2)
        mTabLayout_4?.setTabData(mTabEntities)
        mTabLayout_5?.setTabData(mTabEntities)
        mTabLayout_6?.setTabData(mTabEntities)
        mTabLayout_7?.setTabData(mTabEntities)
        mTabLayout_8?.setTabData(mTabEntities)
        mTabLayout_9?.setTabData(mTabEntities)
        mTabLayout_10?.setTabData(mTabEntities)
        mTabLayout_11?.setTabData(mTabEntities11)
        mTabLayout_msg?.setTabData(mTabEntities)
        mTabLayout_3?.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                mTabLayout_1?.currentTab = position
                mTabLayout_2?.currentTab = position
                mTabLayout_4?.currentTab = position
                mTabLayout_5?.currentTab = position
                mTabLayout_6?.currentTab = position
                mTabLayout_7?.currentTab = position
                mTabLayout_8?.currentTab = position
            }

            override fun onTabReselect(position: Int) {}
        })
        mTabLayout_8?.currentTab = 2
        mTabLayout_3?.currentTab = 1

        //显示未读红点
        mTabLayout_1?.showDot(2)
        mTabLayout_3?.showDot(1)
        mTabLayout_4?.showDot(1)

        //两位数
        mTabLayout_2?.showMsg(0, 55)
        mTabLayout_2?.setMsgMargin(0, -5f, 5f)

        //三位数
        mTabLayout_2?.showMsg(1, 100)
        mTabLayout_2?.setMsgMargin(1, -5f, 5f)

        //设置未读消息红点
        mTabLayout_2?.showDot(2)
        val rtv_2_2 = mTabLayout_2?.getMsgView(2)
        if (rtv_2_2 != null) {
            UnreadMsgUtils.setSize(rtv_2_2, dp2px(7.5f))
        }

        //设置未读消息背景
        mTabLayout_2?.showMsg(3, 5)
        mTabLayout_2?.setMsgMargin(3, 0f, 5f)
        val rtv_2_3 = mTabLayout_2?.getMsgView(3)
        if (rtv_2_3 != null) {
            rtv_2_3.backgroundColor = Color.parseColor("#6D8FB0")
        }
        mTabLayout_msg?.showMsg(0, 55)
        mTabLayout_msg?.setMsgMargin(0, -10f, 10f)

        //三位数
        mTabLayout_msg?.showMsg(1, 100)
        mTabLayout_msg?.setMsgMargin(1, -10f, 10f)
        mTabLayout_msg?.showMsg(2, 100)
        mTabLayout_msg?.setMsgMargin(2, -10f, 10f)
        mTabLayout_msg?.showMsg(3, 100)
        mTabLayout_msg?.setMsgMargin(3, -10f, 10f)
        mTabLayout_msg?.showMsg(4, 100)
        mTabLayout_msg?.setMsgMargin(4, -10f, 10f)

        //第一个.
        ctlLottie.setTabData(
            mutableListOf(
                MenuTabEntity(
                    "anim_tab_drink_on.json",
                    /*"anim_tab_drink_off.json",*/
                    R.mipmap.ic_tab_drink_off
                ),
                MenuTabEntity(
                    "anim_tab_shop_on.json",
                    R.mipmap.ic_tab_shop_off
                ),
                MenuTabEntity(
                    "anim_drink_add.json",
                    R.mipmap.ic_tab_drink_off_b
                ),
                MenuTabEntity(
                    "anim_tab_compute_on.json",
                    R.mipmap.ic_tab_tongji_off
                ),
                MenuTabEntity(
                    "anim_tab_my_on.json",
                    R.mipmap.ic_tab_my_off
                )
            )
        )

        ctlLottie.currentTab = 2

    }

    private fun tl_2() {
        mTabLayout_2!!.setTabData(mTabEntities)
        mTabLayout_2!!.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                mViewPager!!.currentItem = position
            }

            override fun onTabReselect(position: Int) {
                if (position == 0) {
                    mTabLayout_2!!.showMsg(0, mRandom.nextInt(100) + 1)
                    //                    UnreadMsgUtils.show(mTabLayout_2.getMsgView(0), mRandom.nextInt(100) + 1);
                }
            }
        })
        mViewPager!!.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                mTabLayout_2!!.currentTab = position
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        mViewPager!!.currentItem = 1
    }

    protected fun dp2px(dp: Float): Int {
        val scale = mContext.resources.displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }

    private inner class MyPagerAdapter(fm: FragmentManager?) : FragmentPagerAdapter(
        fm!!
    ) {
        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mTitles[position]
        }

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }
    }
}